package com.ofwiki.demo.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ofwiki.demo.model.Jobs;
import com.ofwiki.demo.scheduler.JobRegisterHandler;
import com.ofwiki.demo.service.JobsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Jobs)Controller
 *
 * @author huang.js
 * @version 1.0
 * @date 2023-03-18
 */

@Api(tags = "定时任务")
@RestController
@RequestMapping("/v1/tb_jobs")
@Validated
public class JobsController  {
    @Autowired
    private JobsService jobsService;
    @Autowired
    private JobRegisterHandler jobRegisterHandler;

    @ApiOperation(value = "1.列表查询")
    @GetMapping("list")
    public List<Jobs> list(Jobs req) {
        QueryWrapper<Jobs> query = new QueryWrapper<>(req);
        return jobsService.list(query);
    }
    
    
    @ApiOperation("2.修改")
    @PutMapping("{id}")
    public String update(@RequestBody Jobs job) {
        jobsService.updateById(job);
        jobRegisterHandler.loadJobHandler();
        return "ok";
    }

    @ApiOperation("3.立即执行一次")
    @GetMapping("execute/{id}")
    public String execute(@PathVariable("id") Long id, @RequestParam("param") String param) {
        jobRegisterHandler.triggerJobHandler(id, param);
        return "ok";
    }
}

